package com.corecloud.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.corecloud.entity.SysRoleEntity;
import com.corecloud.entity.SysUserEntity;
import com.corecloud.entity.SysUserRoleEntity;
import com.corecloud.service.SysRoleService;
import com.corecloud.service.SysUserRoleService;
import com.corecloud.service.SysUserService;
import com.corecloud.tools.AjaxResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户角色相关操作
 */
@Controller
@RequestMapping("/awarded")
public class UserRoleController {

	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysUserService sysUserService;

	/**
	 * 用户授予角色页面
	 * @param id
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(Integer id){
		ModelAndView mav = new ModelAndView("awarded/index");
		mav.addObject("userInfo",sysUserService.selectOne(new QueryWrapper<SysUserEntity>().eq("id",id)));
		return mav;
	}

	/**
	 * 获取可以对用户授权的角色
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public List<Map<String,Object>> list(Integer userId){
		QueryWrapper<SysRoleEntity> qw = new QueryWrapper<>();
		qw.eq("is_valid",1);
		//除去超级管理员外其他角色可被绑定至用户
		qw.ne("code","SYS_ADMIN");
		//查询所有可分配角色
		List<Map<String,Object>> roleList = sysRoleService.getMapByParas(qw);
		//根据当前用户ID查询已有角色
		if(userId != null){
			List<SysUserRoleEntity> surList = sysUserRoleService.getListByParas(new QueryWrapper<SysUserRoleEntity>().eq("user_id",userId));
			//重新拼装角色列表数据，为表格复选框做默认选中功能
			if(surList != null && surList.size() > 0){
				for(int i = 0;i<roleList.size();i++){
					Map<String,Object> role = roleList.get(i);
					role.put("LAY_CHECKED",false);
					for(int j = 0;j<surList.size();j++){
						SysUserRoleEntity sur = surList.get(j);
						if(sur.getRoleCode().equals(role.get("code"))){
							role.put("LAY_CHECKED",true);
						}
					}
				}
			}
		}
		return roleList;
	}

	/**
	 * 保存用户绑定角色相关信息
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxResultData save(String rids,Integer userId){
		if(userId == null){ return new AjaxResultData(500,"参数异常,请联系管理员"); }

		SysUserEntity userInfo = sysUserService.selectOne(new QueryWrapper<SysUserEntity>().eq("id",userId));
		if(userInfo == null){ return new AjaxResultData(500,"查无此用户,请确认后再绑定角色"); }

		//开始进行角色绑定
		try {
			//先删除该用户名下所有角色，再重新添加
			QueryWrapper qw = new QueryWrapper<SysUserRoleEntity>();
			qw.eq("user_id",userInfo.getId());
			//超管角色不在操作范围内
			qw.ne("role_code","SYS_ADMIN");
			sysUserRoleService.deleteByParas(qw);
			if(!StringUtils.isEmpty(rids)){
				SysUserRoleEntity sur;
				String[] ridArr = rids.split(",");
				for(int i = 0;i<ridArr.length ;i++){
					sur = new SysUserRoleEntity();
					if(!StringUtils.isEmpty(ridArr[i])){
						SysRoleEntity sr = sysRoleService.getByParas(new QueryWrapper<SysRoleEntity>().eq("id",Integer.parseInt(ridArr[i])));
						sur.setUserId(userInfo.getId());
						sur.setRoleId(sr.getId());
						sur.setRoleCode(sr.getCode());
						sur.setRoleName(sr.getName());
						sur.setRoleState(sr.getIsValid());
						sysUserRoleService.insertInfo(sur);
					}
				}
			}
			return new AjaxResultData("操作成功");
		}catch (Exception e){
			return new AjaxResultData(500,"操作失败,请联系管理员");
		}
	}
}
